import {
  getCarInfoList,
  editCarInfoList
} from '@/api/vehicle'

const state = {
  vehicleData: [],
  vehicleDataCount: 0
}

const mutations = {
  GET_VEHICLE_DATA: (state, data) => {
    state.vehicleData = data
  },
  GET_VEHICLE_DATA_COUNT: (state, data) => {
    state.vehicleDataCount = data
  }
}

const actions = {
  async getVehicleData({ commit }, playload) {
    let data = {}
    let newData = []
    if (Object.keys(playload).length > 0) {
      data = playload
    }
    const resp = await getCarInfoList(data)
    resp.data.data.rows.map((item) => {
      newData = [...newData, {
        id: item.id,
        cartype0: item.cartype0,
        num: item.num,
        cartype1: item.cartype1,
        color: item.color,
        city: item.city,
        regdate: item.regdate,
        yearcare: item.yearcare,
        yearinsure0: item.yearinsure0,
        yearinsure: item.yearinsure,
        ctime: item.ctime,
        state: item.state,
        type0: item.type0,
        type1: item.type1
      }]
    })
    // for (let i = 0; i < 5; i++) {
    //   newData = [...newData, {
    //     id: i + 1,
    //     cid: '010',
    //     number: '苏D-A2929',
    //     brand: '阿斯顿马丁',
    //     ctype: '超豪华轿跑车',
    //     color: '黑色',
    //     logintime: '2019-04-24',
    //     reviewtime: '2019-04-24',
    //     insuretime: '2019-04-24',
    //     businesstime: '2019-04-24',
    //     ctime: '2019-04-24 14:58:00',
    //     address: '江苏常州武进',
    //     state: 0
    //   }]
    // }
    commit('GET_VEHICLE_DATA', newData)
    commit('GET_VEHICLE_DATA_COUNT', resp.data.data.total)
    return resp
  },
  async editVehicleData({ commit }, playload) {
    const resp = await editCarInfoList(playload)
    return resp
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
